updating oE seek

seek

include io.e 
namespace io 
public function seek(file_number fn, file_position pos) 

Seek (move) to any byte position in a file.

Parameters:
  1. fn : an integer, the handle to the file or device to seek
  2. pos : an atom, either an absolute 0-based position or -1 to seek to end of file.
Returns:

An integer, 0 on success, 1 on failure.

Errors:

The target file or device must be open.

Comments:

For each open file, there is a current byte position that is updated as a result of I/O operations on the file. The initial file position is 0 for files opened for read, write or update. The initial position is the end of file for files opened for append. It is possible to seek past the end of a file. If you seek past the end of the file, and write some data, undefined bytes will be inserted into the gap between the original end of file and your new data.

After seeking and reading (writing) a series of bytes, you may need to call seek explicitly before you switch to writing (reading) bytes, even though the file position should already be what you want.

This function is normally used with files opened in binary mode. In text mode, Windows converts CR LF to LF on input, and LF to CR LF on output, which can cause great confusion when you are trying to count bytes because seek counts the Windows end of line sequences as two bytes, even if the file has been opened in text mode.

Example 1:
include std/io.e 
 
integer fn 
fn = open("my.data", "rb") 
-- read and display first line of file 3 times: 
for i = 1 to 3 do 
    puts(STDOUT, gets(fn)) 
    if seek(fn, 0) then 
        puts(STDOUT, "rewind failed!\n") 
    end if 
end for 
See Also:

get_bytes, puts, where

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu